#include "CMouse.h"

cMouse::cMouse(DWORD mouseCoopFlags, IDirectInput8 *pDInput, HWND hWnd)
{
	//Zero out the mouse state
	ZeroMemory(&m_MouseState, sizeof(m_MouseState));

	//Create the mouse device
	pDInput->CreateDevice(GUID_SysMouse, &m_pMouse, 0);

	//Set the data format for the device
	m_pMouse->SetDataFormat(&c_dfDIMouse);

	//Set the cooperative levels for the device
	m_pMouse->SetCooperativeLevel(hWnd, mouseCoopFlags);

	//Acquire the device for usage
	m_pMouse->Acquire();
}

cMouse::~cMouse()
{
	if(m_pMouse)
	{
		m_pMouse->Release();
		m_pMouse = NULL;
	}
}

bool cMouse::isMousePressed(int button)
{
	return m_MouseState.rgbButtons[button] & 0x80;
}

float cMouse::MouseX()
{
	return (float)m_MouseState.lX;
}

float cMouse::MouseY()
{
	return (float)m_MouseState.lY;
}

float cMouse::MouseZ()
{
	return (float)m_MouseState.lZ;
}

void cMouse::Poll()
{
	HRESULT hr = m_pMouse->GetDeviceState(sizeof(DIMOUSESTATE2), 
											(LPVOID)&m_MouseState);

	if(FAILED(hr))
	{
		ZeroMemory(&m_MouseState, sizeof((LPVOID)&m_MouseState));

		m_pMouse->Acquire();
	}
}